package recursion;

public class MaxSizeOfIsland695 {

    public static int maxAreaOfIsland(int[][] grid) {
        int rowNum = grid.length;
        if (rowNum == 0)
            return 0;
        int maxSize = 0;
        int colNum = grid[0].length;
        for (int i = 0; i < rowNum; i++) {
            for (int j = 0; j < colNum; j++) {
                int size = 0;
                size = infect(grid, i, j);
                maxSize = Math.max(maxSize, size);
            }
        }
        return maxSize;
    }

    public static int infect(int[][] grid, int rowIdx, int colIndx) {
        int size = 0;
        if (rowIdx >= grid.length || colIndx >= grid[0].length || rowIdx < 0 || colIndx < 0 || grid[rowIdx][colIndx] == 0)
            return size;

        if (grid[rowIdx][colIndx] == 1) {
            grid[rowIdx][colIndx] = 0;
            size++;
        }
        size += infect(grid, rowIdx + 1, colIndx);
        size += infect(grid, rowIdx, colIndx + 1);
        size += infect(grid, rowIdx - 1, colIndx);
        size += infect(grid, rowIdx, colIndx - 1);
        return size;
    }

    public static void main(String[] args) {
//        System.out.println(maxAreaOfIsland(new int[][]{{1, 1, 0, 0, 0}, {1, 1, 0, 0, 0}, {0, 0, 0, 1, 1}, {0, 0, 0, 1, 1}}));
    int a  = -121;
    for(int  i =0; i < String.valueOf(a).length(); i++)
        System.out.println(String.valueOf(a).charAt(i));
    }
}
